package com.hc.core.jdbc;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

import org.apache.log4j.Logger;

/**
 * jdbc连接类
 * 
 * @author user
 * 
 */
public class Dbcn {
	private static final Logger logger = Logger.getLogger(Dbcn.class);

	// 构造函数
	public Dbcn() {
	}

	public static Connection getConn() {
		Connection conn = null;
		String url = "jdbc/OracleDB";

		try {
			Context env = (Context) new InitialContext()
					.lookup("java:comp/env");
			DataSource pool = (DataSource) env.lookup(url);
			conn = pool.getConnection();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("连接池中建立与数据库连接失败！");
		}
		return conn;
	}

	public static void close(ResultSet rs, PreparedStatement ps1, Connection con) {
		try {
			if (null != rs) {
				rs.close();
			}
			if (null != ps1) {
				ps1.close();
			}
			if (null != con) {
				con.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("关闭数据库连接出错!");
		}
	}
}
